package aidturith.db.configuration;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import aidturith.db.beans.Alias;
import aidturith.db.beans.Music;
import aidturith.db.beans.Subtitle;
import aidturith.db.hibernate.HibernateUtil;
import aidturith.files.ProcessFile;
import aidturith.files.mp3.ID3v2;
import aidturith.files.mp3.InvalidDataException;
import aidturith.files.mp3.Mp3File;
import aidturith.files.mp3.UnsupportedTagException;

public class Importer {

	public static void importFile(Session session, File f) throws UnsupportedTagException, InvalidDataException, IOException {
		// import file info
		Transaction tx = session.beginTransaction();

		aidturith.db.beans.File file_info = new aidturith.db.beans.File();
		file_info.setFile_path(f.getPath());
		file_info.setFile_size(f.length());
		file_info.setFile_tagged(false);
		file_info.setFile_date_modified(f.lastModified());

		// import music info
		Music music = new Music();
		Mp3File mp3file = new Mp3File(f.getAbsolutePath());
		if(mp3file.hasId3v2Tag()) {
			ID3v2 tags = mp3file.getId3v2Tag();
			/*System.out.println("Track: "+tags.getTrack());
			System.out.println("Title: "+tags.getTitle());
			System.out.println("Subtitle: "+tags.getSubtitle());
			System.out.println("Album: "+tags.getAlbum());
			System.out.println("Year: "+tags.getYear());
			System.out.println("Circle: "+tags.getAlbumArtist());
			System.out.println("Artist: "+tags.getArtist());
			System.out.println("Composer: "+tags.getComposer());
			System.out.println("Lyricist: "+tags.getLyricist());
			System.out.println("Genre: "+tags.getGenreDescription());
			System.out.println("Mood: "+tags.getMood());
			System.out.println("Language: "+tags.getLangage());
			System.out.println("BMP: "+tags.getBMP());
			System.out.println("Commentaire: "+tags.getComment());
			System.out.println("Disc: "+tags.getPartOfSet());*/

			// track
			music.setMusic_title(tags.getTitle()); // unicité

			if((tags.getSubtitle() != null)
					&& (tags.getSubtitle() != "")
					&& (tags.getSubtitle() != "Original")
					&& (tags.getSubtitle() != "null")) {
				String subtitle_name = tags.getSubtitle();
				String[] subtitles;

				if(subtitle_name.contains("; "))
					subtitles = subtitle_name.split("; ");
				else if(subtitle_name.contains(" / "))
					subtitles = subtitle_name.split(" / ");
				else if(subtitle_name.contains(" & "))
					subtitles = subtitle_name.split(" & ");
				else {
					subtitles = new String[1];
					subtitles[0] = subtitle_name;
				}

				for(String name : subtitles) {
					name = name.trim();
					Criteria crit = session.createCriteria(Subtitle.class);
					crit.add(Restrictions.ilike("subtitle_name", name));
					List<Subtitle> list = crit.list();

					Criteria crit1 = session.createCriteria(Alias.class);
					crit1.add(Restrictions.ilike("alias_name", name));
					List<Alias> list_alias = crit1.list();

					// check subtitle
					if(list.size() > 0)
						music.addSubtitle(list.get(0));
					// check aliases
					else if(list_alias.size() > 0)
						music.addSubtitle(list_alias.get(0).getSubtitle());
					// new subtitle
					else {
						Subtitle subtitle = new Subtitle(name);
						music.addSubtitle(subtitle);
						session.save(subtitle);
					}
				}
			}

			//music.setAlbum(new Album(tags.getAlbum(), "")); // unicité & détails
			// date?
			// circle?
			// artists
			//music.addComposer(new Artist(tags.getComposer())); // unicité & détails
			//music.addLyrist(new Artist(tags.getLyricist())); // unicité & détails
			//music.addGenre(new Genre(tags.getGenreDescription())); // split & unicité & détails
			// mood
			// language
			// bpm
			// comment
		}
		else if(mp3file.hasId3v1Tag())
			System.out.println("have v1 tags!");

		file_info.setMusic(music);
		session.save(music);
		session.save(file_info);

		tx.commit();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Session session = HibernateUtil.getSession();
		try {
			int i = 0;
			ProcessFile fileProcessor = new ProcessFile("C:\\Users\\admin\\Music");
			for(File f : fileProcessor.getList()) {
				Importer.importFile(session, f);
				System.out.println(++i+"/"+fileProcessor.getList().size());
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (UnsupportedTagException e) {
			e.printStackTrace();
		} catch (InvalidDataException e) {
			e.printStackTrace();
		}
		session.close();
	}

}
